/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: EditGuidelinePresenter.java
 * Date: 04/02/2011
 */

package com.ingenius.sau.client.presenter;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.ingenius.sau.client.AppController;
import com.ingenius.sau.client.Constants;
import com.ingenius.sau.client.event.ListGuidelinesEvent;
import com.ingenius.sau.client.service.GuidelinesServiceAsync;
import com.ingenius.sau.client.view.Display;
import com.ingenius.sau.shared.model.guidelines.Guideline;

/**
 * Class <strong>EditGuidelinePresenter</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class EditGuidelinePresenter implements EditPresenter {

	public interface EditGuidelineDisplay extends Display {

		String getName();

		void setName(String name);

		String getDescription();

		void setDescription(String description);

		String getAuthorName();

		void setAuthorName(String authorName);

		Date getDate();

		void setDate(Date date);

		void show();

		void hide();
	}

	private final HandlerManager eventBus;
	private final GuidelinesServiceAsync rpcService;
	private final EditGuidelineDisplay display;
	private Guideline guideline;

	public EditGuidelinePresenter(HandlerManager eventBus,
			GuidelinesServiceAsync rpcService, EditGuidelineDisplay display,
			String guidelineId) {

		this.rpcService = rpcService;
		this.eventBus = eventBus;
		this.display = display;
		display.setPresenter(this);

		if (guidelineId == null) {
			guideline = new Guideline();

		} else {
			loadBean(guidelineId);
		}
	}

	private void loadBean(String guidelineId) {
		AppController.setStatusMessage("Loading...");
		rpcService.getGuideline(guidelineId, new AsyncCallback<Guideline>() {
			@Override
			public void onSuccess(Guideline result) {
				if (result == null) {
					onFailure(new Throwable("Database error"));
					return;
				}
				guideline = result;
				display.setName(guideline.getName());
				display.setDescription(guideline.getDescription());
				display.setAuthorName(guideline.getAuthorName());
				display.setDate(guideline.getDate());
				hideStatusMessage();
			}

			@Override
			public void onFailure(Throwable caught) {
				GWT.log(Constants.MESSAGE_FAILED_GET
						+ Guideline.class.getName(), caught);
				guideline = new Guideline();
				// TODO i18n
				String failedMessage = "Failed to retrieve guideline";
				display.showInfo(failedMessage);
				hideStatusMessage();
			}

			void hideStatusMessage() {
				AppController.hideStatusMessage();
			}
		});
	}

	@Override
	public void doSaveOrUpdate() {
		guideline.setName(display.getName());
		guideline.setDescription(display.getDescription());
		guideline.setAuthorName(display.getAuthorName());
		guideline.setDate(display.getDate());

		// TODO i18n
		final String failedMessage = "Failed to save guideline";
		final String successMessage = "Guideline saved sucessfully";

		display.hide();
		AppController.setStatusMessage("Saving...");
		rpcService.saveOrUpdateGuideline(guideline,
				new AsyncCallback<Boolean>() {
					@Override
					public void onSuccess(Boolean result) {
						if (result) {
							eventBus.fireEvent(new ListGuidelinesEvent());
							display.showInfo(successMessage);
							hideStatusMessage();

						} else {
							onFailure(new Throwable("Database error"));
						}
					}

					@Override
					public void onFailure(Throwable caught) {
						GWT.log(Constants.MESSAGE_FAILED_SAVE
								+ Guideline.class.getName(), caught);
						display.showInfo(failedMessage);
						hideStatusMessage();
					}

					void hideStatusMessage() {
						AppController.hideStatusMessage();
					}
				});
	}

	@Override
	public void go(HasWidgets container) {
		display.show();
	}

}
